<!-- 角色列表 -->
<template>
  <div class="rolelist">
    <el-col :span="24">
      <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <el-card class="box-card">
      <div class="text item" style="line-height: 0;text-align: left;">
        <el-button type="primary">新增角色</el-button>
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-table style="width: 100%" border>
                <el-table-column label="一级权限" width="150">
                  <template slot-scope="scope">
                    <el-tag v-for="(firstLevel, firstIndex) in props.row.children" :key="firstIndex">
                      {{ firstLevel.authName }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="二级权限" width="200">
                  <template slot-scope="scope">
                    <el-tag v-for="(secondLevel, secondIndex) in props.row.children.flatMap(firstLevel => firstLevel.children)" :key="secondIndex" type="success">
                      {{ secondLevel.authName }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="三级权限">
                  <template slot-scope="scope">
                    <el-tag v-for="(thirdLevel, thirdIndex) in props.row.children.flatMap(firstLevel => 
                      firstLevel.children.flatMap(secondLevel => secondLevel.children || [])
                    )" :key="thirdIndex" type="info">
                      {{ thirdLevel.authName }} <p class="iconfont el-icon-close"></p>
                    </el-tag>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <el-table-column label="">
            <!-- 箭头 -->
            <template slot-scope="scope">
              <el-icon>
                <arrow-right />
              </el-icon>
            </template>
          </el-table-column>
          <el-table-column label="" prop="id">
          </el-table-column>
          <el-table-column label="角色名称" prop="roleName">
          </el-table-column>
          <el-table-column label="描述" prop="roleDesc">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary">编辑</el-button>
              <el-button type="danger">删除</el-button>
              <el-button type="info">设置</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      baseURL: 'http://43.138.15.137:7000/api/private/v1',
      token: localStorage.getItem('token'),
      tableData: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await axios.get(this.baseURL + '/roles', {
        headers: {
          'Authorization': this.token
        }
      }).then(res => {
        let data = res.data.data
        this.tableData = data
        console.log(this.tableData);
        console.log(this.tableData[0].children);
      })
    }
  }
}
</script>
